package com.hhp.cloud.shop.product.controller;

import java.util.Arrays;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.hhp.cloud.shop.product.entity.AttrGroupEntity;
import com.hhp.cloud.shop.product.service.AttrGroupService;
import com.hhp.cloud.shop.common.core.result.R;



/**
 * 属性分组
 *
 * @author hhp
 * @email 2437579794@qq.com
 * @date 2021-12-07 15:15:44
 */
@RestController
@RequestMapping("/api/v1/product/attrGroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    /**
     * 列表
     */
    @ApiOperation(value = "属性分组分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query", dataType = "Long"),
            @ApiImplicitParam(name = "limit", value = "每页数量", paramType = "query", dataType = "Long"),
            @ApiImplicitParam(name = "attrGroupName", value = "组名", paramType = "query", dataType = "String")
    })
    @GetMapping
    public R list(Integer page, Integer limit, String attrGroupName){
        return R.ok().setData(attrGroupService.pageMaps(
                new Page<>(page, limit),
                new LambdaQueryWrapper<AttrGroupEntity>()
                        .like(StrUtil.isNotBlank(attrGroupName), AttrGroupEntity::getAttrGroupName, attrGroupName)));
    }


    /**
     * 信息
     */
    @ApiOperation(value = "属性分组详情")
    @ApiImplicitParam(name = "id", value = "属性分组id", required = true, paramType = "path", dataType = "Long")
    @GetMapping("/{id}")
    public R info(@PathVariable("id") Long attrGroupId){
		return R.ok().setData(attrGroupService.getById(attrGroupId));
    }

    /**
     * 保存
     */
    @ApiOperation(value = "新增属性分组")
    @PostMapping
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @ApiOperation(value = "修改属性分组")
    @PutMapping("/{id}")
    public R update(@PathVariable("id") Long attrGroupId, @RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除属性分组")
    @DeleteMapping
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
